package com.ygqh.customs.controller;

import com.ygqh.customs.model.DataUpListModel;
import com.ygqh.customs.model.ResultSet;
import com.ygqh.customs.request.DataUpRequest;
import com.ygqh.customs.request.DataUpSearchRequest;
import com.ygqh.customs.request.PlatDataOpenRequest;
import com.ygqh.customs.response.PlatDataResponse;
import com.ygqh.customs.service.YgCustomsDataUpService;
import com.ygqh.customs.utils.HttpRequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/3/14 18:17
 * @since 1.0.0
 */
@Slf4j
@RequestMapping("/customs")
@RestController
public class CustomsPlatDataController {

    @Autowired
    private YgCustomsDataUpService ygCustomsDataUpService;

    @GetMapping("/test")
    @ResponseBody
    public String test() {

        return "test";
    }

    @GetMapping("/platDataOpenLocal")
    @ResponseBody
    public PlatDataResponse platDataOpenLocal(PlatDataOpenRequest request) {

        log.info("platDataOpen request:{}", request);

        PlatDataResponse platDataResponse = ygCustomsDataUpService.platDataOpen(request);
        log.info("platDataOpen response:{}", platDataResponse);
        return platDataResponse;
    }

    @RequestMapping("/platDataOpen")
    @ResponseBody
    public PlatDataResponse platDataOpen(HttpServletRequest request) {

        Map<String, String> map = HttpRequestUtils.formToParamMap(request);
        log.info("platDataOpen request:{}", map);
        PlatDataResponse platDataResponse = ygCustomsDataUpService.platDataOpen(map);
        log.info("platDataOpen response:{}", platDataResponse);
        return platDataResponse;
    }


    @PostMapping("/sendDataToSign")
    @ResponseBody
    public PlatDataResponse sendDataToSign(PlatDataOpenRequest request) {
        log.info("sendDataToSign request:{}", request);
        ygCustomsDataUpService.sendDataToSign(request);
        PlatDataResponse response = new PlatDataResponse();
        response.setCode("200");
        response.setMessage("发送成功");
        return response;
    }

    @PostMapping("/realTimeDataUp")
    @ResponseBody
    public PlatDataResponse realTimeDataUp(@RequestBody DataUpRequest dataUpRequest) {

        log.info("platDataOpen request:{}", dataUpRequest);

        PlatDataResponse platDataResponse = ygCustomsDataUpService.platDataUp(dataUpRequest);

        log.info("platDataOpen response:{}", platDataResponse);
        return platDataResponse;
    }

    @PostMapping("/search")
    @ResponseBody
    public Map<String, Object> search(DataUpSearchRequest searchRequest) {
        ResultSet<DataUpListModel> search = ygCustomsDataUpService.search(searchRequest);
        return search.toDatatables(null);
    }
}
